package person.aoao.library.controller;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import person.aoao.library.entity.AdminAcount;
import person.aoao.library.entity.UserList;
import person.aoao.library.result.ResponseCode;
import person.aoao.library.result.ResponseData;
import person.aoao.library.service.UserListService;
import person.aoao.library.vo.ListData;

import javax.annotation.Resource;

/**
 * 用户表(UserList)表控制层
 *
 * @author makejava
 * @since 2023-05-05 10:15:39
 */
@RestController
@CrossOrigin
@RequestMapping("userList")
public class UserListController {
    /**
     * 服务对象
     */
    @Resource
    private UserListService userListService;

    /**
     * 分页查询
     *
     * @param userList    筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<UserList>> queryByPage(UserList userList, PageRequest pageRequest) {
        return ResponseEntity.ok(this.userListService.queryByPage(userList, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<UserList> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.userListService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param userList 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<UserList> add(UserList userList) {
        return ResponseEntity.ok(this.userListService.insert(userList));
    }

    /**
     * 编辑数据
     *
     * @param userList 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<UserList> edit(UserList userList) {
        return ResponseEntity.ok(this.userListService.update(userList));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        System.out.println(id);
        return ResponseEntity.ok(this.userListService.deleteById(id));
    }



    @GetMapping("/getUserList/{type}/{num}/{page}")
    public ResponseData getAdminList(@PathVariable String type, @PathVariable Integer  num, @PathVariable Integer  page){
        ListData list=this.userListService.getUesrList(type,num,page);
        System.out.println();
        return  new ResponseData(ResponseCode.SUCCESS,list);
    }

    @GetMapping("/checkInfo/{type}/{value}")
    public ResponseData checkInfo(@PathVariable String type,@PathVariable String value){
        return this.userListService.checkInfo(type,value);
    }

    @GetMapping("/getUserInfoByUid/{uid}")
    public  ResponseData getUserInfoByUid(@PathVariable String uid){
        UserList list=this.userListService.getUserInfoByUid(uid);
        return new ResponseData(ResponseCode.SUCCESS,list);
    }

    @PostMapping("/deleteUserInfo")
    public  ResponseData deleteUserInfo(@RequestBody UserList userList){
        System.out.println(userList.getUid());
        ResponseData data=this.userListService.deleteUserInfo(userList.getId());
        return data;
    }

    @PostMapping("searchUser/{type}/{num}/{page}")
    public ResponseData searchRole(@RequestBody String value,@PathVariable String type,@PathVariable Integer num, @PathVariable Integer page){
        ListData list=this.userListService.searchUser(value.replace("\"", ""),type,num,page);
        return new ResponseData(ResponseCode.SUCCESS,list);
    }



    @PostMapping("addAdmin")
    public  ResponseData addAdmin(@RequestBody AdminAcount adminAcount){
        ResponseData data=this.userListService.addAdmin(adminAcount.getUid(),adminAcount.getRoleid());
        return  data;
    }

    @GetMapping("getUserDetail/{uid}")
    public ResponseData getUserDetail(@PathVariable String uid){
        ResponseData data=this.userListService.getUserDetail(uid);
        return data;
    }


}

